// pages/lifePay/getCompany/getCompany.js
var Ajax = require('../../../utils/Ajax.js')
var utils = require('../../../utils/util.js');
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    pageType:'',//0水 1电 2燃气
    imgurl: app.getApi('download'),
    queryname:'',
    userId: "",
    tapinput:false,
    searchArr:[],
    nearObj: {
      quartersName: '上海市'
    },
    nearArr: [],
  },
  refresh() { //刷新

  },
  //选择公司
  selectItem(e){
    var { item: { companyName, productNumber} } = e.currentTarget.dataset
    utils.toPage(`/pages/lifePay/queryPrice/queryPrice?type=${this.data.pageType}&companyName=${companyName}&productNumber=${productNumber}`)
  },
  listNew() {
    Ajax.post('getCompany', {
      type: this.data.pageType,
      cityName: '上海',
    }).then((d) => {
      if (d.status == 200 && d.data) {
        this.setData({
          nearArr: d.data || []
        })
      } else {
        utils.showtoast(d.msg || '定位失败,网络错误')
      }
    }).catch((e) => {
      utils.showtoast('定位失败,网络错误')
    })
  },
  delText(e) { //清空input
    this.setData({
      ['' + e.detail]: '',
      tapinput: false,
    })
  },
  tapinput() {
    var { nearArr, searchArr } = this.data;
    this.setData({
      tapinput: true,
      searchArr: searchArr.length > 0 ? searchArr : nearArr,
    })
  },
  bindinputval(e) { //问题描述输入的字符控制
    var {
      name
    } = e.currentTarget.dataset;
    var { nearArr } = this.data;
    var val = e.detail.value;
    val = val.replace(/\s+/g, "");
    this.setData({
      [name]: val
    }, () => {
      var arr = [] // 临时数组 用于存放匹配的数组
      for (let i in nearArr) {
        if (nearArr[i].companyName.indexOf(val) >= 0) {
          arr.push(nearArr[i])
        }
      }
      if (arr.length == 0) {
        this.setData({
          searchArr: nearArr,
        })
      } else {
        this.setData({
          searchArr: arr,
        })
      }
    })
  },
  selectCity() {
    var { nearObj } = this.data;
    // utils.toPage(`/pages/changcity/changcity?city=${nearObj.quartersName}`)
  },
  delText(e) { //清空input
    this.setData({
      ['' + e.detail]: '',
      tapinput: false,
    })
  },
  onReachBottom: function () { },
  onPullDownRefresh: function () {
    this.refresh()
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    const sysInfo = wx.getSystemInfoSync();
    const screenWidth = sysInfo.screenWidth;
    this.factor = screenWidth / 750;
    this.setData({
      pageType: options.type,
      model: app.globalData.model,
    },()=>{
      if(options.type ==0){
        wx.setNavigationBarTitle({
          title: '水费'
        })
      } else if (options.type == 1) {
        wx.setNavigationBarTitle({
          title: '电费'
        })
      }else if (options.type == 2) {
        wx.setNavigationBarTitle({
          title: '燃气费'
        })
      }
      this.listNew()
    })
  },
  onReady() {
    var query = wx.createSelectorQuery();
    query.select('.pages').boundingClientRect((rect) => {
      this.setData({
        nogoodsH: (rect.height / this.factor) - 68 - 60,
      })
    }).exec();
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})